package com.doodod.common.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ydw
 * Date: 15-4-25
 * Time: 下午3:28
 *
 * 登陆相关操作，
 */
public class BaseAction extends ActionSupport  {


    public boolean isLogin() {
        ActionContext actionCtx = ActionContext.getContext();
        Map<String, Object> session = actionCtx.getSession();
        if (session != null && session.containsKey("userName") && session.containsKey("role")) {
            String userName = (String) session.get("userName");
            Integer role = (Integer) session.get("role");
            if (null != userName && !"".equals(userName) && null != role && role > 3)
                return true;
        }
        return false;
    }

    /**
     * 判断当前用户是否正常登陆
     *
     * @return
     */
    public boolean isNormalLogin() {
        ActionContext actionCtx = ActionContext.getContext();
        Map<String, Object> session = actionCtx.getSession();
        if (session != null && session.containsKey("userEmail") && session.containsKey("token")) {
            String userEmail = session.get("userEmail").toString();
            String token = session.get("token").toString();
            if (null != userEmail && !"".equals(userEmail) && null != token && !"".equals(token))
                return true;
        }
        return false;
    }

    /**
     * 判断当前用户是否为超级管理账户
     *
     * @return
     */
    public boolean isSuperAdmin() {
        ActionContext actionCtx = ActionContext.getContext();
        Map<String, Object> session = actionCtx.getSession();
        if (session != null && session.containsKey("userName") && session.containsKey("role")) {
            String userName = (String) session.get("userName");
            Integer role = (Integer) session.get("role");
            if (null != userName && !"".equals(userName) && null != role && role > 5)
                return true;
        }
        return false;
    }

    public int getRole() {
        ActionContext actionCtx = ActionContext.getContext();
        Map<String, Object> session = actionCtx.getSession();
        if (session != null && session.containsKey("userName") && session.containsKey("role")) {
            return (Integer) session.get("role");
        }
        return 0;
    }



}
